package com.sg.service.biz.component.nb;

import cn.hutool.core.bean.BeanUtil;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.biz.component.req.ObtainDataFilterSpecRecByReqDto;
import com.sg.dto.biz.component.res.ObtainDataFilterSpecRecByRespDto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbOperationsDeployment {
    /**
     * code:dynamic_lookup_data
     * name:M2查询动态寻表数据列表(特殊方法)
     * desc:undefined
     * gen by moon at 6/11/2023, 5:27:46 PM
     **/
    @Trace(operationName = "M2查询动态寻表数据列表(特殊方法)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryDynamicLookupDataComListRespDto queryDynamicLookupDataComList(QueryDynamicLookupDataComListReqDto reqDto) {
        QueryDynamicLookupDataComListRespDto retData = new QueryDynamicLookupDataComListRespDto();
        Map<String, String> filter = new HashMap<>();
        try {
            Class classzz = reqDto.getClass();
            Field[] declaredFields = classzz.getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                if (field.get(reqDto) != null && !field.getName().equals("toolTableEncryptionName")) {
                    String fieldName = CommonFunctionHelper.humpToUnderline(field.getName());
                    filter.put(fieldName, (String) field.get(reqDto));
                }
            }

            if (filter.size() > 0) {
                List<Map<String, String>> data = CommonFunctionHelper.getDbData(reqDto.getToolTableEncryptionName(), filter);
                for (Map<String, String> one : data) {
                    DynamicLookupDataDto elm = new DynamicLookupDataDto();
                    Class elmClasszz = elm.getClass();
                    Field[] elmDeclaredFields = elmClasszz.getDeclaredFields();
                    for (Field field : elmDeclaredFields) {
                        field.setAccessible(true);
                        String fieldName = CommonFunctionHelper.humpToUnderline(field.getName());
                        if (one.containsKey(fieldName)) {
                            field.set(elm, one.get(fieldName));
                        }
                    }
                    retData.getDynamicLookupDataList().add(elm);
                }
            }
        } catch (Exception e) {

        }


        return retData;
    }

    /**
     * code:dynamicLookupDeleteData
     * name:M2批量动态寻表删除数据(特殊方法)
     * desc:undefined
     * gen by moon at 6/11/2023, 5:28:05 PM
     **/
    @Trace(operationName = "M2批量动态寻表删除数据(特殊方法)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public BatchDeleteDynamicLookupDeleteDataRespDto batchDeleteDynamicLookupDeleteData(BatchDeleteDynamicLookupDeleteDataReqDto reqDto) {
        BatchDeleteDynamicLookupDeleteDataRespDto retData = new BatchDeleteDynamicLookupDeleteDataRespDto();
        CommonFunctionHelper.batchDelDbData(reqDto.getToolTableEncryptionName(), reqDto.getDynamicLookupDataList(), CommonFunctionHelper.humpToUnderline(reqDto.getEntityName()));
        return retData;
    }

    /**
     * code:dataFilterSpecRecBy
     * name:M2获取数据集按入参过滤指定记录(特殊方法)
     * desc:undefined
     * gen by moon at 6/12/2023, 11:21:49 AM
     **/
    @Trace(operationName = "M2获取数据集按入参过滤指定记录(特殊方法)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainDataFilterSpecRecByRespDto obtainDataFilterSpecRecBy(ObtainDataFilterSpecRecByReqDto reqDto) {
        ObtainDataFilterSpecRecByRespDto retData = new ObtainDataFilterSpecRecByRespDto();
        for (DataFilterSpecRecByDto one : reqDto.getDataFilterSpecRecByList()) {
            if (!one.equals(reqDto.getComTxtField())) {
                retData.getDataFilterSpecRecByList().add(one);
            }
        }
        return retData;
    }

    /**
     * code:receptionService
     * name:M2-获取空间列表
     * desc:undefined
     * gen by moon at 6/12/2023, 11:29:42 PM
     **/
    @Trace(operationName = "M2-获取空间列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ObtainReceiveFieldsRespDto obtainReceiveFields(ObtainReceiveFieldsReqDto reqDto) {
        return BeanUtil.toBean(reqDto, ObtainReceiveFieldsRespDto.class);
    }
    //手工接入方法
}
